﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ScreenManagement;


namespace PokeMania.Screens
{
    public class Menu : GameScreen
    {

        #region Fields

        ContentManager content;
        Texture2D backgroundTexture;

        float selectionFade;
        Vector2 position;
        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public Menu()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }


        /// <summary>
        /// Loads graphics content for this screen. The background texture is quite
        /// big, so we use our own local ContentManager to load it. This allows us
        /// to unload before going from the menus into the game itself, wheras if we
        /// used the shared ContentManager provided by the Game class, the content
        /// would remain loaded forever.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            backgroundTexture = content.Load<Texture2D>("Graphics/start");
        }


        /// <summary>
        /// Unloads graphics content for this screen.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the background screen. Unlike most screens, this should not
        /// transition off even if it has been covered by another screen: it is
        /// supposed to be covered, after all! This overload forces the
        /// coveredByOtherScreen parameter to false in order to stop the base
        /// Update method wanting to transition off.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
            selectionFade = Math.Min(selectionFade + fadeSpeed, 1);


            base.Update(gameTime, otherScreenHasFocus, false);
        }

        public override void HandleInput(InputState input)
        {
            if (input.CurrentKeyboardStates.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Space))
                LoadingScreen.Load(ScreenManager, true, 0, new MainMenu());

            base.HandleInput(input);
        }

        /// <summary>
        /// Draws the background screen.
        /// 
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

            spriteBatch.Begin();

            Color color = Color.Black * TransitionAlpha;
            double time = gameTime.TotalGameTime.TotalSeconds;
            float pulsate = (float)Math.Sin(time * 6) + 1;
            float scale = 1 + pulsate * 0.05f * selectionFade;
            Vector2 position = new Vector2(100f, 300f);

            spriteBatch.Draw(backgroundTexture, fullscreen, new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));


            position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - ScreenManager.Font.MeasureString(Ressourcen.Content.IntroMessage).X / 2;
            spriteBatch.DrawString(ScreenManager.Font, Ressourcen.Content.IntroMessage, position, color, 0, new Vector2(0, ScreenManager.Font.LineSpacing / 2), scale, SpriteEffects.None, 0);




            spriteBatch.End();
        }


        #endregion
    }
}
